package com.miyake.loveviewweb.client.plugin;

import java.util.ArrayList;
import java.util.List;

import com.google.appengine.api.datastore.Key;
import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.SimplePanel;
import com.miyake.loveviewweb.client.dialog.DialogHandler;
import com.miyake.loveviewweb.client.dialog.IDialogPanel;
import com.miyake.loveviewweb.client.service.DataService;
import com.miyake.loveviewweb.client.service.DataServiceAsync;
import com.miyake.loveviewweb.shared.model.PluginModel;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.event.dom.client.ClickEvent;

public class PluginRegister extends Composite implements HasText, IDialogPanel {

    private static PluginRegisterUiBinder uiBinder = GWT
        .create(PluginRegisterUiBinder.class);
    @UiField TextBox textURL;
    @UiField Button buttonRegister;
    @UiField SimplePanel tablePanel;
    @UiField Button buttonDelete;
    @UiField Button buttonClear;

    private IPluginList pluginList;
    private DataServiceAsync service = DataService.Util.getInstance();
    interface PluginRegisterUiBinder extends UiBinder<Widget, PluginRegister> {
    }

    public PluginRegister() {
        initWidget(uiBinder.createAndBindUi(this));
        initialize();
    }

    public PluginRegister(String firstName) {
        initWidget(uiBinder.createAndBindUi(this));
        initialize();
    }

    private void initialize() {
        tablePanel.clear();
        pluginList = new PluginListDataGrid();
        //pluginList.setHeight("200px");
        tablePanel.add(pluginList.getWidgetUi());
        String category = "";
        String tag = "";
        pluginList.clear();
        service.getMyPluginList(category, tag, new AsyncCallback<List<PluginModel>>() {

            public void onFailure(Throwable caught) {

            }

            public void onSuccess(List<PluginModel> result) {
                pluginList.addData(PluginModelEx.getPluginModelEx(result));
            }
            
        });  
    }

    public void setText(String text) {
        textURL.setText(text);
    }

    public String getText() {
        return textURL.getText();
    }
    @UiHandler("buttonRegister")
    void onButtonRegisterClick(ClickEvent event) {
        service.registerPlugin(textURL.getText(), new AsyncCallback<Key>() {

            public void onFailure(Throwable caught) {
                Window.alert(caught.getMessage());
            }

            public void onSuccess(Key result) {
                
                initialize();
            }
            
        });
    }
    @UiHandler("buttonClear")
    void onButtonClearClick(ClickEvent event) {
        service.clearPlugins(new AsyncCallback<Void>() {

            public void onFailure(Throwable caught) {
                // TODO Auto-generated method stub
                
            }

            public void onSuccess(Void result) {
                initialize();
            }
            
        });
    }
    @UiHandler("buttonDelete")
    void onButtonDeleteClick(ClickEvent event) {
        List<PluginModelEx> data = pluginList.getData();
        List<Key> keys = new ArrayList<Key>();
        for (PluginModelEx p : data) {
            if (p.isSelected()) {
                keys.add(p.getPluginModel().getKey());
            }
        }
        service.removePlugins(keys, new AsyncCallback<Void>() {

            public void onFailure(Throwable caught) {
                // TODO Auto-generated method stub
                
            }

            public void onSuccess(Void result) {
                initialize();
            }
            
        });
    }

    public void setDialogHandler(DialogHandler handler) {
        // TODO Auto-generated method stub
        
    }

    public Composite getComposite() {
        return this;
    }
}
